package com.framework.applogic;

import com.framework.util.Browser;
import com.framework.util.PropertyLoader;
import com.framework.webdriver.WebDriverFactory;
import org.openqa.selenium.WebDriver;

public class ApplicationManager {

    private UserHelper userHelper;
    private MovieHelper movieHelper;
    private NavigationHelper navHelper;
    private WebDriver driver;
    private String username;
    private String password;
    private String gridHubUrl;
    private String baseUrl;
    private Browser browser;

    public ApplicationManager() {

        //Save data from application.properties
        username = PropertyLoader.loadProperty("user.username");
        password = PropertyLoader.loadProperty("user.password");
        gridHubUrl = PropertyLoader.loadProperty("grid2.hub");
        baseUrl = PropertyLoader.loadProperty("site.url");
        browser = new Browser();
        browser.setName(PropertyLoader.loadProperty("browser.name"));
        browser.setVersion(PropertyLoader.loadProperty("browser.version"));
        browser.setPlatform(PropertyLoader.loadProperty("browser.platform"));

        //Create browser
        driver = WebDriverFactory.getInstance(gridHubUrl, browser, username, password);

        //Create helpers
        userHelper = new UserHelper(this);
        movieHelper = new MovieHelper(this);
        navHelper = new NavigationHelper(this);

        //Open main page
        driver.get(baseUrl);
    }

    //Getters
    public UserHelper getUserHelper() {
        return userHelper;
    }

    public MovieHelper getMovieHelper() {
        return movieHelper;
    }

    public NavigationHelper getNavigationHelper() {
        return navHelper;
    }

    protected WebDriver getWebDriver() {
        return driver;
    }

    protected String getBaseUrl() {
        return baseUrl;
    }

    //Close browser
    public void stop() {
        if (driver != null) {
            driver.quit();
        }
    }
}
